<?php
class profileAction extends galaxyAction{
    public function index(){
    	$this->checkLogin();
    	$this->LIST = M('mblog_profile')->where(array('uid'=>$_ENV['g']->uid))->select();
    	$this->display();
    }
    public function json() {
    	if($_ENV['g']->uid<=0) {
	    	$this->ajaxReturn('',L('login_please'),-1);
    	}
    	switch($_GET['ac']) {
    		case 'get':
    			$page = intval($_GET['page']);
    			$pp = intval($_GET['pp']);
    			$page = max(1, $page);
    			$pp = max(10, $pp);
    			
    			$start = ($page-1)*$pp;
    			$this->ajaxReturn(M('mblog_profile')->where(array('uid'=>$_ENV['g']->uid))->limit($start, $pp)->select());
    			break;
    		default:
	    		$this->ajaxReturn('',L('bad_submit'),-1);
    			break;
    	}
    }
    private function checkConflict($mblog_user, $action) {
    	switch($action) {
    		case 'r':
    			return M('mblog_profile')->where(array(
    				'uid'=>$_ENV['g']->uid,
    				'mblog_user'=>$mblog_user,
    				'type'=>array('in'=>array('w', 'r')))
    			)->count()>0 ? true : false;
    		case 'w':
    			return M('mblog_profile')->where(array(
    				'uid'=>$_ENV['g']->uid,
    				'mblog_user'=>$mblog_user,
    				'type'=>array('in'=>array('w', 'r')))
    			)->count()>0 ? true : false;
    		case 'u':
    			return M('mblog_profile')->where(array(
    				'uid'=>$_ENV['g']->uid,
    				'mblog_user'=>$mblog_user,
    				'type'=>array('in'=>array('u')))
    			)->count()>0 ? true : false;
    		default:
    			throw_exception('mblog_profile_unknown_action');
    			return false;
    	}
    }
    public function edit() {
    	$this->checksubmit(true)->checkLogin();
	    $M = M('mblog_profile');
	    $arr = $M->find($_POST['mblog_profile_id']);
	    if(!$arr || $arr['uid'] != $_ENV['g']->uid) $this->showMessage('bad_submit');
	    
    	if(($_POST['mblog_user']!=$arr['mblog_user'] || $_POST['type'] != $arr['type']) && self::checkConflict($_POST['mblog_user'], $_POST['action'])) {
    		$this->showMessage('profile_conflict_user');
    	}
    	$data = unserialize($arr['data']);
    	if(strpos('wu', $_POST['action'])!==false && $_POST['pass']) {
    		$data['pass'] = galaxy::authcode($_POST['pass'], 'ENCODE', 0, C('SITE_KEY').'mblog');
    	}
    	unset($_POST['pass']);
    	//unset($_POST['submit']);
    	$_POST['data'] = serialize($data);
    	$_POST['uid'] = $_ENV['g']->uid;
    	$M->save($_POST);
    	$this->showMessage('operation_success'.$_POST['submit'], $_GET['ajax'] ? 'javascript:location.reload()' : '');
    }
    public function del() {
    	$this->checksubmit(true)->checkLogin();
	    $M = M('mblog_profile');
	    $arr = $M->find($$_POST['mblog_profile_id']);
	    if(!$arr || $arr['uid'] != $_ENV['g']->uid) $this->showMessage('bad_submit');
	    
    	$M->delete($_POST['mblog_profile_id']);
	    $this->showMessage('operation_success', $_GET['ajax'] ? 'javascript:location.reload()' : '');
	}
    public function add() {
    	$this->checksubmit(true)->checkLogin();
    	if(!$_POST['mblog_user']) $this->showMessage('profile_noempty_user');
    	if(self::checkConflict($_POST['mblog_user'], $_POST['action'])) {
    		$this->showMessage('profile_conflict_user');
    	}
    	
    	//make
    	$data = array();
    	if(strpos('wu', $_POST['action'])!==false) {
    		$data['pass'] = galaxy::authcode($_POST['pass'], 'ENCODE', 0, C('SITE_KEY').'mblog');
    	}
    	unset($_POST['pass']);
    	$_POST['data'] = serialize($data);
    	$_POST['uid'] = $_ENV['g']->uid;
    	M('mblog_profile')->add($_POST);
    	$this->showMessage('profile_add_success', $_GET['ajax'] ? 'javascript:location.reload()' : '');
    }
}
?>